package com.hack4jp.hackearthquakes;

import twitter4j.Twitter;
import twitter4j.auth.RequestToken;
import twitter4j.GeoLocation;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import android.content.Intent;
import android.net.Uri;

public class TwitterObject {
	
	protected GeoLocation _location = null;
	protected Twitter _twitter = null;

	private RequestToken _requestToken = null;
	
	public Twitter getTwitter()
	{
		return _twitter;
	}
	
	public void createLocation(double latitude, double longitude)
	{
		_location = new GeoLocation(latitude, longitude);
	}
	
	public GeoLocation getLocation()
	{
		return _location;
	}
	
	public boolean twitterAvailable()
	{
		if (_twitter != null)
			return true;
	
		return false;
	}

	public String AccessToken() throws Exception
	{
		String accessToken = Preferences.getInstance().read(Preferences.OAUTH_ACCESS_TOKEN);
		return accessToken;
	}
	
	public String AccessTokenSecret() throws Exception
	{
		String accessTokenSecret = Preferences.getInstance().read(Preferences.OAUTH_ACCESS_TOKEN_SECRET);
		return accessTokenSecret;
	}
	
	public boolean tokenAvailable() throws Exception
	{	
		if (AccessToken().length() > 0 && AccessTokenSecret().length() > 0) {
			return true;
		}
		
		return false;
	}
	
	public void createFactory()
	{
		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setOAuthConsumerKey(Config.TWITTER_CONSUMER_KEY);
		cb.setOAuthConsumerSecret(Config.TWITTER_CONSUMER_SECRET);
		cb.setIncludeEntitiesEnabled(true);
		
		try {
			if (tokenAvailable())
			{
				cb.setOAuthAccessToken(AccessToken());  
				cb.setOAuthAccessTokenSecret(AccessTokenSecret()); 
			}
		} catch (Exception e) {
			// e.printStackTrace();
		}

		Configuration conf = cb.build();
		_twitter = new TwitterFactory(conf).getInstance();
	}
	
	public String getAuthorizationURL(String callbackUri) throws TwitterException
	{
		createFactory();
		
		_requestToken = _twitter.getOAuthRequestToken(callbackUri);
		String url = _requestToken.getAuthorizationURL();
		
		return url;
	}
	
	public Intent startOAuth(String callbackUri) throws TwitterException
	{
		String url = getAuthorizationURL(callbackUri);
		Intent i = new Intent(Intent.ACTION_VIEW , Uri.parse(url));

		return i;
	}
	
	protected void parseOAuthParameter(Uri uri) throws TwitterException, Exception
	{
		AccessToken aToken = null;
		String verifier = uri.getQueryParameter("oauth_verifier");
		if(verifier == null || verifier.length() == 0){
			throw new Exception("authentication error");
		}

		aToken = _twitter.getOAuthAccessToken(_requestToken, verifier);
		
		Preferences.getInstance().write(Preferences.OAUTH_ACCESS_TOKEN, aToken.getToken());
		Preferences.getInstance().write(Preferences.OAUTH_ACCESS_TOKEN_SECRET, aToken.getTokenSecret());
	}
	
	protected TwitpicUpload getTwitpicUploadObject() throws Exception
	{
		TwitpicUpload twitpic = new TwitpicUpload(Config.TWITTER_CONSUMER_KEY, Config.TWITTER_CONSUMER_SECRET,
				AccessToken(), AccessTokenSecret(), Config.TWITPIC_API_KEY);
		
		return twitpic;
	}
}
